<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 产品
 * Class
 * @package
 */
class Prouduct extends Api
{
	protected $noNeedLogin = [];
	protected $noNeedRight = '*';
	/**
	 * 列表
	 */
	public function lists()
	{
		$ret = Db::name("product")
			->field("id,name,image")
			->select();
		if ($ret) {
			$data = [["id" => 0, "name" => "全部"]];
			foreach ($ret as $k => $v) {
				$ret[$k]["image"] = "http://" . $_SERVER["SERVER_NAME"] . $v["image"];
			}
			$data = array_merge($data, $ret);
			$this->success("正常", $data);
		} else {
			$this->error("暂无数据");
		}
	}

	/**
	 * 费率修改-终端列表
	 */
	public function modi_rate_terminal_lists()
	{
		$uid = $this->auth->id;
		$proid = $this->request->post("proid");
		$typs =  $this->request->post("typs"); // 【D 默认|T 调价】费率
		$isMods = $this->request->post("isMods"); // 区分 0 可操作 1 不可操作
		$sn = $this->request->post("sn"); // 区分 0 可操作 1 不可操作
		$page = $this->request->post("page"); // 区分 0 可操作 1 不可操作
		$data = [];
		// 默认
		if ($typs == "D") {
			$data = \app\api\controller\product\Terminal::instance()->modi_default_rate_data($uid, $proid, $typs, $page, $isMods, $sn);
		}
		// 调价
		if ($typs == "T") {
			$data = \app\api\controller\product\Terminal::instance()->modi_read_price_rate_data($uid, $proid, $typs, $page, $isMods, $sn);
		}
		$this->success("正常", ["counts" => count($data), "data" => $data]);
	}

	/**
	 * 获取 费改模板
	 */
	public function fee_temps()
	{
		$proid = $this->request->post("proid");
		$typs =  $this->request->post("typs"); 	// 【D 默认|T 调价】费率 
		$sn = $this->request->post("sn"); 		// 终端号 :【默认费率  传空字符串 ， 调价费率 传 SN】
		$data = [];
		$ret = \app\api\controller\product\Terminal::instance()->rate_mods_template($proid, $typs, $sn);
		if ($ret) {
			$data = $ret;
		}
		$this->success("正常", $data);
	}

	/**
	 * 费改 - 确认
	 */
	public function fee_mods_loading()
	{
		$uid = $this->auth->id;
		$proid = $this->request->post("proid");
		$typs = $this->request->post("typs");  	// 【D 默认|T 调价】费率 
		$sn = $this->request->post("sn");
		$appyRate = $this->request->post("appyRate");
		$origRate = $this->request->post("origRate");

		$retMsg = Config("msg");
		$retCode = "0306";
		if ($appyRate != $origRate) {
			$retCode = \app\api\controller\product\Terminal::instance()->rate_mods_operate($uid, $proid, $typs, $sn, $appyRate, $origRate);
			if ($retCode == "0300") {
				$this->success($retMsg[$retCode], 1);
			}
		}
		$this->error($retMsg[$retCode], 0);
	}
}
